package com.skin.admin.controller;


import com.skin.admin.common.FenyeSetting;
import com.skin.admin.entity.Fenye;
import com.skin.admin.entity.Users;
import com.skin.admin.manager.RoleManager;
import com.skin.admin.manager.UsersManager;
import com.skin.configuration.shiro.ShiroLoad;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Skin
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/users")
public class UsersController {

    @Autowired
    private UsersManager usersManager;

    @Autowired
    private RoleManager roleManager;

    @Autowired
    private ShiroLoad shiroLoad;

    @RequestMapping(value = "/getAll")
    public ModelAndView getAll(HttpServletRequest request){
        Fenye f= FenyeSetting.setfenye(request);
        ModelAndView mv =new ModelAndView();
        mv.addObject("Users",usersManager.findAll(f.getCurrent(),f.getSize()));
        mv.setViewName("admin/users/users");
        return mv;
    }

    @RequestMapping(value = "/findById/{id}")
    public ModelAndView findById(@PathVariable("id") String id) {
        ModelAndView mv =new ModelAndView();
        mv.addObject("Users",usersManager.findBy("id",id));
        mv.setViewName("admin/users/users");
        return mv;
    }

    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable int id){
        usersManager.deleteById(id);
        return "redirect:/users/getAll";
    }

    /**
     * 编辑
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit/{id}")
    public String edit(@PathVariable String id,Model model){
            Users users =usersManager.findBy("id",id);
            if(users!=null){
                model.addAttribute("Users",users);
            }
        return "admin/users/usersedit";
    }

    /**
     * 编辑
     * @param users
     * @return
     */
    @RequestMapping(value = "/editDo")
    public String editDo(Users users){
        usersManager.edit(users);
        return "redirect:/users/getAll";
    }

    /**
     * 新增
     */
    @RequestMapping(value = "/insert")
    public String insert(){
        return "admin/users/usersinsert";
    }

    @RequestMapping(value = "/insertDo")
    public String insertDo(Users users){
        usersManager.insert(users);
        return "redirect:/users/getAll";
    }

    /**
     * 用户权限分配
     */
    @RequestMapping(value = "/userconfig")
    public ModelAndView userconfig(@RequestParam("id") String id){
        ModelAndView mv =new ModelAndView();
        mv.addObject("roles",roleManager.All());
        mv.addObject("userid",id);
        mv.addObject("hasroles",roleManager.findUserroleBy("userid",id));
        mv.setViewName("admin/users/usersconfig");
        return mv;
    }
    @RequestMapping(value = "/userconfigDo",method = RequestMethod.POST)
    @ResponseBody
    public int userconfigDo(@RequestParam("userid") String userid, @RequestParam("roleid") String roleid){
        int i =usersManager.userroleconfig(userid,roleid);
        if(i==1){
            shiroLoad.updatePermission();
        }
        return i;
    }
}

